package com.magus.project.yddemo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.magus.framework.core.exception.app.ServiceException;
import com.magus.framework.core.persistence.JpaSearchUtils;
import com.magus.framework.core.persistence.SearchFilter;
import com.magus.framework.core.repository.BaseRepository;
import com.magus.framework.core.service.BaseService;
import com.magus.project.yddemo.entity.IndicatorMapperConfig;
import com.magus.project.yddemo.repository.IndicatorMapperConfigRepository;

/**
 * 指标映射配置 Service
 * 
 * @author xujuan
 * @date 2018-01-19 15:26:53
 */
@Service
@Transactional(readOnly = true)
public class IndicatorMapperConfigService extends BaseService<IndicatorMapperConfig, String> {

	@Autowired
	private IndicatorMapperConfigRepository indicatorMapperConfigRepository;

	@Override
	protected BaseRepository<IndicatorMapperConfig, String> getBaseRepository() {
		return this.indicatorMapperConfigRepository;
	}
	
	@Override
	@Transactional
	public IndicatorMapperConfig save(IndicatorMapperConfig indicatorMapperConfig) {
		List<SearchFilter> searchFilters = new ArrayList<>();
		searchFilters.add(SearchFilter.equal("code", indicatorMapperConfig.getCode()));
		Specification<IndicatorMapperConfig> spec = JpaSearchUtils.buildAndSpec(searchFilters);
		IndicatorMapperConfig config = indicatorMapperConfigRepository.findOne(spec);
		if (config != null && !indicatorMapperConfig.equals(config)) {
			throw new ServiceException("APP:YDDEMO:INDICATORMAPPERCONFIG_ADD", "已存在的指标编码",
					"ControllerException.indicatorMapperConfig.exist",
					indicatorMapperConfig.getName());
		}
		
		return this.indicatorMapperConfigRepository.save(indicatorMapperConfig);
	}

}
